home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ShareWare OnLine 2
/
ShareWare OnLine Volume 2 (CMS Software)(1993).iso
/
win1
/
fres20.zip
/
FRESSRC.EXE
/
FRES.C
next >
Wrap
C/C++ Source or Header
|
1993-03-23
|
16KB
|
523 lines
//--------------------------------------------------------------------------
//
// Fres: Free Resources Display Application
// Displays: Date, Time, Windows Mode, Global free memory,
// limiting resource heap, and percentage of free resource heap.
//
// Demonstrates the use of a non-modal dialog as the main window,
// the use of the undocumented function: GetHeapSpaces (Win 3.0),
// and the use of GetFreeSystemsResources (Win 3.1).
//
// Design & Coding March 1992 by Jamie O'Connell
// Copyright (c) 1992 by Jamie O'Connell
//
// 921025 JWO: Now displays free diskspace
// 930321 JWO: Added Configuration Dialog -- MM Sounds
//
//--------------------------------------------------------------------------
#include "windows.h"
#include "mmsystem.h"
#include <time.h>
#include <string.h>
#include <dos.h>
#include <stdlib.h>
#include <direct.h>
#include "fres.h"
//---------------------------------------------------------------------------
// Global Variables...
//---------------------------------------------------------------------------
HINSTANCE hInstance; // Global instance handle for application
HWND hwndKill;
UINT idKill;
int Millisec = 500; // Current timer speed in milliseconds
WORD SwitchCt = 6; // Number of timer interrupts before switch
HWND hwndMain; // Main hwnd. Needed in callback
char szDT[32]; // storage to build strings
char szMem[32]; // storage to build strings
char szAppName[] = "Fres"; // Name of App
char szVerStr[16];
char szOnTopKey[16]; // Profile key
char szTmFMKey[16];
char szTmFDKey[16];
char szDiskKey[16];
char szSndNmKey[16];
char szSndStKey[16];
char szSndPath[128];
char szMenuBuf[16];
char szTmFreeMem[8] = "3";
char szTmFreeDisk[8] = "3";
char szDriveNm[8] = "C";
char szSndState[8];
WORD wSndState = IDSND0;
WORD wTimeFM;
WORD wTimeFD;
WORD wDriveNum = 3; // Drive C
short ChimeCt = 0;
short Tick = 0;
BOOL fOnTop = FALSE;
HMENU hSysMenu;
RECT rPos;
DLGPROC lpfnAbtDlg;
DLGPROC lpfnCfgDlg;
DLGPROC lpfnDlgProc;
//---------------------------------------------------------------------------
// Function declarations
//---------------------------------------------------------------------------
BOOL FAR PASCAL DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL FAR PASCAL CfgDlg(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL FAR PASCAL AbtDlg(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL FormatDT(char *szStr);
BOOL FormatMem(char *szStr);
WORD HeapInfo(VOID);
WORD Hp31Info(VOID);
//---------------------------------------------------------------------------
// WinMain
//---------------------------------------------------------------------------
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev,
LPSTR lpstrCmdLine, int cmdShow) {
MSG msgMain;
char tmp[6];
char szDrive[_MAX_DRIVE];
char szDir[_MAX_DIR];
char szFName[_MAX_FNAME];
char szExt[_MAX_EXT];
// Set the global instance variable
hInstance = hInst;
LoadString(hInst, IDVERSTR, szVerStr, sizeof(szVerStr));
LoadString(hInst, IDTMFM, szTmFMKey, sizeof(szTmFMKey));
wTimeFM = GetProfileInt(szAppName, szTmFMKey, DFTTMFM);
LoadString(hInst, IDTMFD, szTmFDKey, sizeof(szTmFDKey));
wTimeFD = GetProfileInt(szAppName, szTmFDKey, DFTTMFD);
LoadString(hInst, IDDISK, szDiskKey, sizeof(szDiskKey));
GetProfileString(szAppName, szDiskKey, "C", szDriveNm, sizeof(szDriveNm));
wDriveNum = (WORD)(((char)*szDriveNm)-'A'+1);
lpfnDlgProc = (DLGPROC)MakeProcInstance((FARPROC)DlgProc, hInst);
if ((hwndMain = CreateDialog(hInst, szAppName,
(HWND)NULL, lpfnDlgProc)) == (HWND)NULL)
return(0);
// Add About Item to Sytem menu
hSysMenu = GetSystemMenu(hwndMain, FALSE);
AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
LoadString(hInst, IDM_ABOUT, szMenuBuf, sizeof(szMenuBuf));
AppendMenu(hSysMenu, MF_STRING, IDM_ABOUT, szMenuBuf);
LoadString(hInst, IDM_CONFIG, szMenuBuf, sizeof(szMenuBuf));
AppendMenu(hSysMenu, MF_STRING, IDM_CONFIG, szMenuBuf);
LoadString(hInst, IDM_ONTOP, szMenuBuf, sizeof(szMenuBuf));
AppendMenu(hSysMenu, MF_STRING, IDM_ONTOP, szMenuBuf);
LoadString(hInst, IDM_SAVLOC, szMenuBuf, sizeof(szMenuBuf));
AppendMenu(hSysMenu, MF_STRING, IDM_SAVLOC, szMenuBuf);
LoadString(hInst, IDONTOP, szOnTopKey, sizeof(szOnTopKey));
GetProfileString(szAppName, szOnTopKey, "off", tmp, sizeof(tmp));
if (_stricmp(tmp, "on") == 0 ||
_stricmp(tmp, "yes") == 0 ||
_stricmp(tmp, "true") == 0 ||
_stricmp(tmp, "1") == 0) {
fOnTop = TRUE;
CheckMenuItem(hSysMenu, IDM_ONTOP, MF_CHECKED);
}
if (waveOutGetNumDevs() > 0) {
LoadString(hInst, IDSNDST, szSndStKey, sizeof(szSndStKey));
wSndState = GetProfileInt(szAppName, szSndStKey, IDSND1);
LoadString(hInst, IDSNDNM, szSndNmKey, sizeof(szSndNmKey));
GetProfileString("Sounds", szSndNmKey, "Undefined",
szSndPath, sizeof(szSndPath));
}
if (wSndState != IDSND0) {
if (lstrcmp(szSndPath, "Undefined") == 0) { // Need to define it
GetModuleFileName(hInst, szSndPath, sizeof(szSndPath));
_splitpath(szSndPath, szDrive, szDir, szFName, szExt);
LoadString(hInst, IDWAVNM, szFName, sizeof(szFName));
_makepath(szSndPath, szDrive, szDir, szFName, ".WAV");
strcat(szSndPath,",Fres Chime");
WriteProfileString("Sounds", szSndNmKey, szSndPath);
}
sndPlaySound(szSndNmKey, SND_ASYNC);
}
// Main message "pump"
while (GetMessage((LPMSG) &msgMain, NULL, 0, 0)) {
if (!IsDialogMessage(hwndMain, (LPMSG)&msgMain)) {
TranslateMessage((LPMSG) &msgMain);
DispatchMessage((LPMSG) &msgMain);
}
}
FreeProcInstance((FARPROC)lpfnDlgProc);
return(0);
}
//===========================================================================
// AbtDlg
//
// Dialog Callback
//
//===========================================================================
BOOL FAR PASCAL _export AbtDlg(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_INITDIALOG:
SetDlgItemText(hwnd, IDVERSTR, szVerStr);
return(TRUE);
case WM_CLOSE:
EndDialog(hwnd, 0);
return(TRUE);
case WM_COMMAND:
switch(wParam) {
case IDOK:
EndDialog(hwnd, 0);
return(TRUE);
}
break;
}
return(FALSE);
}
//===========================================================================
// CfgDlg
//
// Dialog Callback
//
//===========================================================================
BOOL FAR PASCAL _export CfgDlg(HWND hDlg, UINT msg,
WPARAM wParam, LPARAM lParam) {
BOOL fTrans;
switch(msg) {
case WM_INITDIALOG:
SetDlgItemInt(hDlg, IDTMFM, wTimeFM, FALSE);
SetDlgItemInt(hDlg, IDTMFD, wTimeFD, FALSE);
wsprintf(szDriveNm, "%c", (wDriveNum+'A'-1));
SetDlgItemText(hDlg, IDDISK, szDriveNm);
SendDlgItemMessage(hDlg, wSndState, BM_SETCHECK, TRUE, 0);
return(TRUE);
case WM_CLOSE:
EndDialog(hDlg, 0);
return(TRUE);
case WM_COMMAND:
switch(wParam) {
case IDOK:
wTimeFM = GetDlgItemInt(hDlg, IDTMFM, &fTrans, FALSE);
wTimeFD = GetDlgItemInt(hDlg, IDTMFD, &fTrans, FALSE);
GetDlgItemText(hDlg, IDDISK, szDriveNm, sizeof(szDriveNm));
wDriveNum = ((char)*szDriveNm)-'A'+1;
if (SendDlgItemMessage(hDlg, IDSND1,
BM_GETCHECK, 0, 0) != 0L)
wSndState = IDSND1; // Every hour
else if (SendDlgItemMessage(hDlg, IDSND2,
BM_GETCHECK, 0, 0) != 0L)
wSndState = IDSND2; // Every 1/2 hour
else if (SendDlgItemMessage(hDlg, IDSND0,
BM_GETCHECK, 0, 0) != 0L)
wSndState = IDSND0; // Off
wsprintf(szTmFreeMem, "%d", wTimeFM);
WriteProfileString(szAppName, szTmFMKey, szTmFreeMem);
wsprintf(szTmFreeDisk, "%d", wTimeFD);
WriteProfileString(szAppName, szTmFDKey, szTmFreeDisk);
wsprintf(szDriveNm, "%c", (wDriveNum+'A'-1));
WriteProfileString(szAppName, szDiskKey, szDriveNm);
wsprintf(szSndState, "%d", wSndState);
WriteProfileString(szAppName, szSndStKey, szSndState);
EndDialog(hDlg, 0);
return(TRUE);
case IDCANCEL:
EndDialog(hDlg, 0);
return(TRUE);
}
break;
}
return(FALSE);
}
//===========================================================================
// WndProc
//
// Window procedure for the sample applications window.
//
//===========================================================================
BOOL FAR PASCAL _export DlgProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
int xpos, ypos, nWidth, nHeight;
WORD wCheck;
char tmp[6];
switch(msg) {
case WM_INITDIALOG:
GetWindowRect(hwnd, (LPRECT)&rPos);
nWidth = rPos.right - rPos.left;
nHeight = rPos.bottom - rPos.top;
xpos = GetProfileInt(szAppName, "X", rPos.left);
ypos = GetProfileInt(szAppName, "Y", rPos.top);
MoveWindow(hwnd, xpos, ypos, nWidth, nHeight, TRUE);
lpfnAbtDlg = (DLGPROC)MakeProcInstance((FARPROC)AbtDlg,
hInstance);
lpfnCfgDlg = (DLGPROC)MakeProcInstance((FARPROC)CfgDlg,
hInstance);
if (SetTimer(hwnd, 1, Millisec, NULL) == NULL) {
MessageBox(hwnd, "Couldn't create timer.", "Notice!", MB_OK);
DestroyWindow(hwnd);
PostQuitMessage(0);
return(TRUE);
}
hwndKill = hwnd;
idKill = 1;
case WM_TIMER:
if (fOnTop & GetWindow(hwnd, GW_HWNDPREV) != hwnd)
SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
if (FormatDT(szDT)) {
SetWindowText(hwnd, szDT); // Time has changed
PostMessage(hwnd, WM_NCPAINT, 0, 0L); // Redo border
}
if (FormatMem(szMem))
SetDlgItemText(hwnd, IDMEM, szMem);
if (ChimeCt > 0) {
if (Tick == 0) {
sndPlaySound(szSndNmKey, SND_ASYNC);
--ChimeCt;
}
Tick = ++Tick % 4; // Modulus 4
}
else
Tick = 0; // Reset this
return(TRUE);
case WM_CLOSE:
KillTimer(hwndKill, idKill);
idKill = 0;
FreeProcInstance((FARPROC)lpfnAbtDlg);
FreeProcInstance((FARPROC)lpfnCfgDlg);
DestroyWindow(hwnd);
PostQuitMessage(0);
return(TRUE);
case WM_SYSCOMMAND:
switch(wParam) {
case IDM_ABOUT:
DialogBox(hInstance, "About", hwnd, lpfnAbtDlg);
return(TRUE);
case IDM_CONFIG:
DialogBox(hInstance, "Config", hwnd, lpfnCfgDlg);
return(TRUE);
case IDM_ONTOP:
if (fOnTop) {
fOnTop = FALSE;
wCheck = MF_UNCHECKED;
strcpy(tmp, "off");
}
else {
fOnTop = TRUE;
wCheck = MF_CHECKED;
strcpy(tmp, "on");
}
CheckMenuItem(hSysMenu, IDM_ONTOP, wCheck);
WriteProfileString(szAppName, szOnTopKey, tmp);
return(TRUE);
case IDM_SAVLOC:
GetWindowRect(hwndMain, (LPRECT)&rPos);
wsprintf((LPSTR)tmp, "%d", rPos.left);
WriteProfileString(szAppName, "X", tmp);
wsprintf((LPSTR)tmp, "%d", rPos.top);
WriteProfileString(szAppName, "Y", tmp);
return(TRUE);
}
break;
}
return(FALSE);
}
//---------------------------------------------------------------------------
// FormatDT
//
// Formats The Date and time - Returns TRUE if it has changed.
//---------------------------------------------------------------------------
BOOL FormatDT(char *szStr) {
static short savehr = -1;
static short savemin = -1;
struct tm *tym;
time_t timer = 0;
short hour;
short mini;
char suffix[5];
timer = time(&timer);
tym = localtime(&timer);
hour = tym->tm_hour;
mini = tym->tm_min;
if ((mini == savemin) && (hour == savehr)) // hasn't changed enough
return FALSE;
savemin = mini;
savehr = hour;
if (hour > 12) {
hour = hour - 12;
strcpy(suffix, "pm");
}
else if (hour == 12) {
if (mini == 0)
strcpy(suffix, "noon");
else
strcpy(suffix, "pm");
}
else {
strcpy(suffix, "am");
if (hour == 0) {
hour = 12;
if (mini == 0)
strcpy(suffix, "nite");
}
}
if ((mini == 0) && (wSndState != IDSND0)) // Hourly
ChimeCt = hour;
if ((mini == 30) && (wSndState == IDSND2)) // 1/2 hour
ChimeCt = 1;
wsprintf((LPSTR)szStr, "%2.2d-%2.2d-%2.2d %2d:%2.2d %s",
tym->tm_mon+1, tym->tm_mday, tym->tm_year,
hour, mini, (LPSTR)suffix);
return TRUE;
}
//---------------------------------------------------------------------------
// FormatMem
//
// Formats the free memory string - Returns TRUE if it's changed.
//---------------------------------------------------------------------------
BOOL FormatMem(char *szStr) {
static WORD wResav = 0;
static WORD savemem = 0;
static WORD Display = DSMEM;
static WORD wCounter = 0; // Counter to display when timer goes off
static long savavail = 0;
static struct diskfree_t dfree;
long avail;
short nres;
short nmod;
WORD wResult;
WORD mem;
BOOL changed = FALSE;
if (wTimeFM == 0)
Display = DSDISK;
else if (wTimeFD == 0)
Display = DSMEM;
else {
switch(Display) {
case DSMEM: // Counter goes off every 1/2 second
if (++wCounter >= (wTimeFM << 1)) { // Decide When to switch displays
wCounter = 0;
Display = DSDISK; // will flip between 0 and 1
changed = TRUE;
}
break;
case DSDISK:
if (++wCounter >= (wTimeFD << 1)) {
wCounter = 0;
Display = DSMEM; // will flip between 0 and 1
changed = TRUE;
}
break;
}
}
switch(Display) {
case DSMEM:
mem = (WORD)(GetFreeSpace(0) >> 10); // get #of KBytes
if (mem != savemem) {
changed = TRUE;
savemem = mem;
}
wResult = HeapInfo();
nres = HIBYTE(wResult);
nmod = LOBYTE(wResult);
if (wResult != wResav) {
changed |= TRUE;
wResav = wResult;
}
if (changed)
wsprintf(szStr, "%s: %5u K %s: %2.2d%%",
(LPSTR)((GetWinFlags() & WF_ENHANCED) ? "Enh" : "Std"),
mem, (LPSTR)((nmod == GDIHP) ? "GDI" : "User"), nres);
break;
case DSDISK:
_dos_getdiskfree(wDriveNum, &dfree);
avail = ((long) dfree.avail_clusters
* (long) dfree.bytes_per_sector
* (long) dfree.sectors_per_cluster) >> 10;
if (avail != savavail) {
changed = TRUE;
savavail = avail;
}
if (changed)
wsprintf(szStr, "Drive %1s: %7lu K",
(LPSTR)szDriveNm, avail);
break;
}
return changed;
}
//---------------------------------------------------------------------------
// HeapInfo
//
// Determines the sizes and percentages of the 2 resource heaps
//
//---------------------------------------------------------------------------
WORD HeapInfo(VOID) {
BYTE PctGDI, PctUser;
WORD RetVal;
PctGDI = GetFreeSystemResources(GDIHP);
PctUser = GetFreeSystemResources(USERHP);
if (PctUser < PctGDI)
RetVal = (WORD)((PctUser << 8) | USERHP);
else
RetVal = (WORD)((PctGDI << 8) | GDIHP);
return RetVal;
}